package com.example.travelgd.util;

import lombok.Data;
import java.io.Serializable;
import java.util.Date;

/**
 * @description 统一返回
 */
@Data
public class FwResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    private int status;
    private String message;
    boolean success;
    Date time;
    private T data;
    private String token;
    private int id;

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public static <T> FwResult<T> ok() {
        return restResult(null, FwConstant.SUCCESS, true, FwConstant.MSG_SUCCESS);
    }

    public static <T> FwResult<T> okMsg(String msg) {
        return restResult(null, FwConstant.SUCCESS, true, msg);
    }

    public static <T> FwResult<T> ok(T data) {
        return restResult(data, FwConstant.SUCCESS, true, FwConstant.MSG_SUCCESS);
    }

    public static <T> FwResult<T> ok(T data, String msg) {
        return restResult(data, FwConstant.SUCCESS, true, msg);
    }

    public static <T> FwResult<T> okMeta(T data) {
        return restResult(data, FwConstant.SUCCESS, true, null);
    }

    public static <T> FwResult<T> ok(T data, String msg, Object meta) {
        return restResult(data, FwConstant.SUCCESS, true, msg);
    }

    public static <T> FwResult<T> failed() {
        return restResult(null, FwConstant.FAIL, false, FwConstant.MSG_FAIL);
    }

    public static <T> FwResult<T> failedMsg(String msg) {
        return restResult(null, FwConstant.FAIL, false, msg);
    }

    public static <T> FwResult<T> failedMsg(int code, String msg) {
        return restResult(null, code, false, msg);
    }

    public static <T> FwResult<T> tokenFailedMsg(String msg) {
        return restResult(null, FwConstant.RELOGIN, false, msg);
    }

    public static <T> FwResult<T> urlNotFoundMsg(String msg) {
        return restResult(null, FwConstant.NOTFOUND, false, msg);
    }

    public static <T> FwResult<T> failed(T data) {
        return restResult(data, FwConstant.FAIL, false, FwConstant.MSG_FAIL);
    }

    public static <T> FwResult<T> failed(T data, String msg) {
        return restResult(data, FwConstant.FAIL, false, msg);
    }

    private static <T> FwResult<T> restResult(T data, int code, boolean success, String msg) {
        FwResult<T> fwResult = new FwResult<>();
        fwResult.setStatus(code);
        fwResult.setSuccess(success);
        fwResult.setData(data);
        fwResult.setMessage(msg);
        fwResult.setTime(new Date());
        return fwResult;
    }

    private static <T> FwResult<T> restResult(T data, int code, boolean success, String token, int id, String msg) {
        FwResult<T> fwResult = new FwResult<>();
        fwResult.setStatus(code);
        fwResult.setSuccess(success);
        fwResult.setData(data);
        fwResult.setMessage(msg);
        fwResult.setTime(new Date());
        fwResult.setToken(token);
        fwResult.setId(id);
        return fwResult;
    }

    public static <T> FwResult<T> ok(String token, int id, String msg) {
        FwResult<T> fwResult = new FwResult<>();
        fwResult.setStatus(FwConstant.SUCCESS);
        fwResult.setSuccess(true);
        fwResult.setToken(token);
        fwResult.setId(id);
        fwResult.setMessage(msg);
        fwResult.setTime(new Date());
        return fwResult;
    }
}